import UIKit

class DelegateAdapter: MarkdownViewDelegate {
    private weak var delegate: MarkdownContentViewDelegate?
    private weak var contentView: MarkdownContentView?
    private let logger = MarkdownLogger.shared
    
    init(delegate: MarkdownContentViewDelegate?, contentView: MarkdownContentView?) {
        self.delegate = delegate
        self.contentView = contentView
        logger.debug("""
        DelegateAdapter initialized:
        - Delegate: \(String(describing: delegate))
        - ContentView: \(String(describing: contentView))
        """)
    }
    
    func markdownView(didTapImage url: URL) {
        guard let contentView = contentView else {
            logger.warning("""
            No contentView available for image tap:
            - URL: \(url)
            """)
            return
        }
        
        logger.info("""
        Image tapped:
        - URL: \(url)
        - ContentView: \(contentView)
        """)
        
        delegate?.markdownContentView(contentView, didTapImage: url)
    }
    
    func markdownView(didLoadImage image: UIImage, from url: URL) {
        guard let contentView = contentView else {
            logger.warning("""
            No contentView available for image loading success:
            - URL: \(url)
            - Image size: \(image.size)
            """)
            return
        }
        
        logger.info("""
        Image loaded successfully:
        - URL: \(url)
        - Size: \(image.size)
        - Scale: \(image.scale)
        """)
        
        delegate?.markdownContentView(contentView, didLoadImage: image, from: url)
    }
    
    func markdownView(didFailToLoadImage url: URL, error: Error) {
        guard let contentView = contentView else {
            logger.warning("""
            No contentView available for image load failure:
            - URL: \(url)
            - Error: \(error.localizedDescription)
            """)
            return
        }
        
        logger.error(error)
        logger.error("""
        Failed to load image:
        - URL: \(url)
        - Error: \(error.localizedDescription)
        - ContentView: \(contentView)
        """)
        
        delegate?.markdownContentView(contentView, failedToHandleLink: url, error: error)
    }
    
    func markdownView(didTapLink url: URL) {
        guard let contentView = contentView else {
            logger.warning("""
            No contentView available for link tap:
            - URL: \(url)
            """)
            return
        }
        
        logger.info("""
        Link tapped:
        - URL: \(url)
        - Scheme: \(url.scheme ?? "none")
        - Host: \(url.host ?? "none")
        """)
        #warning("test")
        //delegate?.markdownContentView(contentView, didTapLink: url)
    }
    
    func markdownView(shouldHandleLink url: URL) -> Bool {
        guard let contentView = contentView else {
            logger.warning("""
            No contentView available for link handling check:
            - URL: \(url)
            """)
            return true
        }
        
        logger.debug("""
        Checking if should handle link:
        - URL: \(url)
        - Scheme: \(url.scheme ?? "none")
        - Host: \(url.host ?? "none")
        """)
        
        return delegate?.markdownContentView(contentView, shouldHandleLink: url) ?? true
    }
    
    func markdownView(didHandleLink url: URL) {
        guard let contentView = contentView else {
            logger.warning("""
            No contentView available for link handling success:
            - URL: \(url)
            """)
            return
        }
        
        logger.info("""
        Link handled successfully:
        - URL: \(url)
        - Scheme: \(url.scheme ?? "none")
        - Host: \(url.host ?? "none")
        """)
        
        delegate?.markdownContentView(contentView, didHandleLink: url)
    }
    
    func markdownView(didFailToHandleLink url: URL, error: Error) {
        guard let contentView = contentView else {
            logger.warning("""
            No contentView available for link handling failure:
            - URL: \(url)
            - Error: \(error.localizedDescription)
            """)
            return
        }
        
        logger.error(error)
        logger.error("""
        Failed to handle link:
        - URL: \(url)
        - Scheme: \(url.scheme ?? "none")
        - Host: \(url.host ?? "none")
        - Error: \(error.localizedDescription)
        """)
        
        delegate?.markdownContentView(contentView, failedToHandleLink: url, error: error)
    }
} 
